package com.google.gwt.sample.dynatablerf.client.views;

import java.util.Set;

import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.sample.dynatablerf.client.presenters.MultipleSelectionDataPresenter.MultipleSelectionDataDisplay;
import com.google.gwt.sample.dynatablerf.client.theme.GrifoTheme;
import com.google.gwt.sample.dynatablerf.client.util.GrifoConstants;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoCheckBoxColumn;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoCheckBoxColumnHeader;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoSearchBox;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoSuggestOracle;
import com.google.gwt.sample.dynatablerf.client.widgets.HasGrifoSuggestHandler;
import com.google.gwt.sample.dynatablerf.client.widgets.HasMenuItens;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConstants;
import com.google.gwt.sample.dynatablerf.shared.ServicoProjetoProxy;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent.HasSelectionChangedHandlers;

public class ServicoProjetoListView extends
		AbstractListView<ServicoProjetoProxy> implements
		MultipleSelectionDataDisplay<ServicoProjetoProxy> {

	private class ServicoDescricaoColumn extends
			Column<ServicoProjetoProxy, String> {
		public ServicoDescricaoColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(ServicoProjetoProxy object) {
			if (object.getServico() != null)
				return object.getServico().getDescricao();
			else
				return null;
		}
	}

	private class ServicoNumeroColumn extends
			Column<ServicoProjetoProxy, String> {
		public ServicoNumeroColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(ServicoProjetoProxy object) {
			if (object.getServico() != null)
				return object.getServico().getCodigo();
			else
				return null;
		}
	}

	private class ServicoQuantidadeColumn extends
			Column<ServicoProjetoProxy, Number> {

		public ServicoQuantidadeColumn() {
			super(new NumberCell());
		}

		@Override
		public Double getValue(ServicoProjetoProxy object) {
			return object.getQuantidade();
		}
	}

	private class ServicoValorColumn extends
			Column<ServicoProjetoProxy, Number> {

		public ServicoValorColumn() {
			super(new NumberCell());
		}

		@Override
		public Double getValue(ServicoProjetoProxy object) {
			return object.getValor();
		}
	}

	private DataGrid<ServicoProjetoProxy> servicoDataGrid;
	private MultiSelectionModel<ServicoProjetoProxy> servicoSelectionModel;
	private SimplePager servicoPager;
	private Image closeTabImage;
	private GrifoSuggestOracle grifoSuggestOracle;
	private GrifoSearchBox grifoSearchBox;

	public ServicoProjetoListView() {
		FlowPanel mainpanel = new FlowPanel();
		initWidget(mainpanel);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setWidth("100%");

		mainpanel.add(horizontalPanel);

		servicoDataGrid = GrifoTheme.getGridPadrao(ServicoProjetoProxy.class);
		servicoDataGrid.setPageSize(GrifoSharedConstants.GRID_NUM_ROWS);

		servicoPager = new SimplePager();
		servicoPager.setDisplay(servicoDataGrid);

		grifoSuggestOracle = new GrifoSuggestOracle();
		grifoSearchBox = GrifoTheme.getSearchBox(grifoSuggestOracle);
		grifoSearchBox.setText(GrifoConstants.INSTANCE.pesquisaRapida());
		horizontalPanel.add(grifoSearchBox);

		horizontalPanel.add(servicoPager);

		servicoDataGrid.setSize("100%", "200px");
		mainpanel.add(servicoDataGrid);

		servicoSelectionModel = new MultiSelectionModel<ServicoProjetoProxy>();
		servicoDataGrid.setSelectionModel(servicoSelectionModel);

		GrifoCheckBoxColumn<ServicoProjetoProxy> checkServicoColumn = new GrifoCheckBoxColumn<ServicoProjetoProxy>(
				servicoSelectionModel);
		GrifoCheckBoxColumnHeader<ServicoProjetoProxy> boxColumnHeader = new GrifoCheckBoxColumnHeader<ServicoProjetoProxy>(
				servicoDataGrid, servicoSelectionModel);
		servicoDataGrid.addColumn(checkServicoColumn, boxColumnHeader);
		servicoDataGrid.setSelectionModel(servicoSelectionModel,
				DefaultSelectionEventManager
						.<ServicoProjetoProxy> createCheckboxManager());

		ServicoNumeroColumn ServicoNumeroColumn = new ServicoNumeroColumn();
		servicoDataGrid.addColumn(ServicoNumeroColumn,
				GrifoConstants.INSTANCE.numero());

		ServicoDescricaoColumn ServicoDescricaoColumn = new ServicoDescricaoColumn();
		servicoDataGrid.addColumn(ServicoDescricaoColumn,
				GrifoConstants.INSTANCE.descricao());

		ServicoQuantidadeColumn ServicoQuantidadeColumn = new ServicoQuantidadeColumn();
		servicoDataGrid.addColumn(ServicoQuantidadeColumn,
				GrifoConstants.INSTANCE.quantidade());

		ServicoValorColumn ServicoValorColumn = new ServicoValorColumn();
		servicoDataGrid.addColumn(ServicoValorColumn,
				GrifoConstants.INSTANCE.valor());

		closeTabImage = GrifoTheme.getCloseTabImage();
	}

	@Override
	public Widget getTabHeader() {
		Label label = GrifoTheme.getLabelPadrao();
		label.setText(GrifoConstants.INSTANCE.servicos());

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(label);
		horizontalPanel.add(closeTabImage);
		return horizontalPanel;
	}

	@Override
	public HasClickHandlers getCloseTabClickHandlers() {
		return closeTabImage;
	}

	@Override
	public HasSelectionHandlers<Suggestion> getHasSuggestionSelectionHandlers() {
		return grifoSearchBox;
	}

	@Override
	public HasGrifoSuggestHandler getGrifoSuggestHandler() {
		return grifoSuggestOracle;
	}

	@Override
	public HasSelectionChangedHandlers getSelectionChangedHandlers() {
		return servicoSelectionModel;
	}

	@Override
	public HasData<ServicoProjetoProxy> getData() {
		return servicoDataGrid;
	}

	@Override
	public Set<ServicoProjetoProxy> getSelectedSet() {
		return servicoSelectionModel.getSelectedSet();
	}

	@Override
	public HasMenuItens getHasMenuItens() {
		return grifoSearchBox;
	}

}
